//
//  ANMineAppointmentCell.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/23.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANMineAppointmentCell.h"
#import "ANGradientButton.h"

@interface ANMineAppointmentCell ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *shopNameLab;
@property (nonatomic, strong) UILabel *stateLab;
@property (nonatomic, strong) UIView *contentBgView;
@property (nonatomic, strong) UILabel *goodsNameLab;
@property (nonatomic, strong) UILabel *goodsName;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UILabel *remLab;
@property (nonatomic, strong) UILabel *rem;

@property (nonatomic, strong) ANGradientButton *cancelBtn;

@end

@implementation ANMineAppointmentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setEntity:(AppointmentEntity *)entity
{
    _entity = entity;
    self.shopNameLab.text = entity.consumerName;
    self.goodsName.text = entity.goodsName;
    self.time.text = entity.time;
    self.rem.text = String(entity.rem);
    if (entity.timeState.intValue == 1) {//可取消
        [self addSubview:self.cancelBtn];
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-kWidth(15));
            make.top.equalTo(self.contentBgView.mas_bottom).offset(kWidth(8));
            make.bottom.equalTo(self).offset(-kWidth(10));
            make.width.equalTo(@(kWidth(80)));
            make.height.equalTo(@(kWidth(30)));
        }];
        self.stateLab.text = @"有效";
        self.stateLab.textColor = UIColorFromRGB(0x257CFF);
    }
    else {//不可取消
        [self.cancelBtn removeFromSuperview];
        self.stateLab.text = @"已过期";
        self.stateLab.textColor = kColor333333;
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews
{
    self.icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"store"]];
    [self addSubview:self.icon];
    
    self.shopNameLab = [[UILabel alloc] init];
    self.shopNameLab.font = [UIFont boldSystemFontOfSize:15];
    self.shopNameLab.textColor = kColor333333;
    [self addSubview:self.shopNameLab];
    
    self.stateLab = [[UILabel alloc] init];
    self.stateLab.font = [UIFont systemFontOfSize:15];
    self.stateLab.textColor = kColor333333;
    [self addSubview:self.stateLab];
    
    self.contentBgView = [[UIView alloc] init];
    self.contentBgView.backgroundColor = UIColorFromRGB(0xF5F9FF);
    [self addSubview:self.contentBgView];
    
    self.goodsNameLab = [[UILabel alloc] init];
    self.goodsNameLab.font = [UIFont systemFontOfSize:14];
    self.goodsNameLab.textColor = kColor666666;
    self.goodsNameLab.text = @"预约项目";
    [self.contentBgView addSubview:self.goodsNameLab];
    
    self.goodsName = [[UILabel alloc] init];
    self.goodsName.font = [UIFont systemFontOfSize:14];
    self.goodsName.textColor = kColor333333;
    self.goodsName.numberOfLines = 0;
    [self.contentBgView addSubview:self.goodsName];
    
    self.timeLab = [[UILabel alloc] init];
    self.timeLab.font = [UIFont systemFontOfSize:14];
    self.timeLab.textColor = kColor666666;
    self.timeLab.text = @"预约时间";
    [self.contentBgView addSubview:self.timeLab];
    
    self.time = [[UILabel alloc] init];
    self.time.font = [UIFont systemFontOfSize:14];
    self.time.textColor = kColor333333;
    self.time.numberOfLines = 0;
    [self.contentBgView addSubview:self.time];
    
    self.remLab = [[UILabel alloc] init];
    self.remLab.font = [UIFont systemFontOfSize:14];
    self.remLab.textColor = kColor666666;
    self.remLab.text = @"备注";
    [self.contentBgView addSubview:self.remLab];
    
    self.rem = [[UILabel alloc] init];
    self.rem.font = [UIFont systemFontOfSize:14];
    self.rem.textColor = kColor333333;
    self.rem.numberOfLines = 0;
    [self.contentBgView addSubview:self.rem];
    
    self.cancelBtn = [[ANGradientButton alloc] initWithFrame:CGRectMake(0, 0, kWidth(80), kWidth(30))];
    [self.cancelBtn setTitle:@"取消预约" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    self.cancelBtn.layer.cornerRadius = kWidth(15);
    self.cancelBtn.layer.masksToBounds = YES;
    
    [self.cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kWidth(45));
        make.left.equalTo(self).offset(kWidth(15));
        make.right.equalTo(self).offset(-kWidth(15));
        make.bottom.equalTo(self).offset(-kWidth(15)).priority(200);
    }];
    
    [self.goodsNameLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.goodsNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentBgView).offset(kWidth(15));
        make.top.equalTo(self.contentBgView).offset(kWidth(15));
    }];
    
    [self.goodsName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsNameLab);
        make.left.equalTo(self.goodsNameLab.mas_right).offset(kWidth(10));
        make.right.lessThanOrEqualTo(self.contentBgView).offset(-kWidth(15));
    }];
    
    [self.timeLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsNameLab);
        make.top.equalTo(self.goodsName.mas_bottom).offset(kWidth(12));
    }];
    
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLab);
        make.left.equalTo(self.timeLab.mas_right).offset(kWidth(10));
        make.right.lessThanOrEqualTo(self.contentBgView).offset(-kWidth(15));
    }];
    
    [self.remLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.remLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsNameLab);
        make.top.equalTo(self.time.mas_bottom).offset(kWidth(12));
    }];
    
    [self.rem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.remLab);
        make.left.equalTo(self.remLab.mas_right).offset(kWidth(10));
        make.right.lessThanOrEqualTo(self.contentBgView).offset(-kWidth(15));
        make.bottom.equalTo(self.contentBgView).offset(-kWidth(15));
    }];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kWidth(17));
        make.bottom.equalTo(self.contentBgView.mas_top).offset(-kWidth(10));
    }];
    
    [self.shopNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.icon);
        make.left.equalTo(self.icon.mas_right).offset(kWidth(7));
        make.right.lessThanOrEqualTo(self.stateLab.mas_left).offset(-kWidth(10));
    }];
    
    [self.stateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-kWidth(15));
        make.centerY.equalTo(self.icon);
    }];
    
    
}

- (void)cancelAction:(id)sender
{
    if (self.cancelHandler) {
        self.cancelHandler();
    }
}

@end
